package com.example.myapplication.service;

import android.util.Log;
import com.example.myapplication.bean.User;
import com.example.myapplication.bean.Response;
import com.example.myapplication.util.HttpUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LoginService {

    private static final String TAG = "LoginService";
    private String responseUserJSONData = null;
    private static final String URL = "http://101.43.110.183:8080/user";


    /**
     * 得到后台传来的userJSON信息返回user
     *
     * @param userName
     */
    public User getResponseUserInfo(String userName) {
        try {
            responseUserJSONData = getUserJsonString(userName);
        } catch (InterruptedException e) {
            Log.d(TAG, "getResponseUserInfo: " + e);
        }
        if (responseUserJSONData != null) {
            Gson gson = new Gson();

            Type typeJson = new TypeToken<Response<User>>() {}.getType();
            Response<User> response = gson.fromJson(responseUserJSONData, typeJson);
            if (response != null && response.getCode() == 200) {
                return response.getData();
            }
        }
        return null;
    }

    public String getUserJsonString(String userName) throws InterruptedException {
        Map<String, String> params = new HashMap<>();
        params.put("name", userName);
        return HttpUtils.get(URL, params);
    }


}
